package com.soft.sitegenerator.ftp;

import java.awt.BorderLayout;
import java.awt.dnd.DropTarget;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;

public class UploadFrame {
	private static final long serialVersionUID = 1L;

	JPanel jContentPane = null;
	private JButton upload = null;
	private FtpUploader upl;  
	private JPanel jButtonsPanel = null;
	private JLabel jStatusLabel = null;
	JProgressBar jProgressBar = null;
	private JPanel jStatusPanel = null;
	JTree jItemsTree = null;
	private DefaultMutableTreeNode rootNode;
	private DefaultTreeModel treeModel;
	private Thread uploadThread;

	private DropTarget target;

	private DropFilesHandler handler;

	/**
	 * This method initializes this
	 *
	 * @return void
	 */
	public void init() {
	}

	public DropFilesHandler getDropHandler() {
		if(this.handler == null) {
			this.handler = new DropFilesHandler(this, this.treeModel, this.rootNode);
		}
		return this.handler;
	}
	
	/**
	 * This method initializes jContentPane
	 *
	 * @return javax.swing.JPanel
	 */
	public JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();

			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJButtonsPanel(), BorderLayout.SOUTH);
			jContentPane.add(getJItemsTree(), BorderLayout.CENTER);
			new DropTarget(jItemsTree, getDropHandler());
			new DropTarget(jContentPane, getDropHandler());
		}
		return jContentPane;
	}

	/**
	 * This method initializes upload
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getUpload() {
		if (upload == null) {
			upload = new JButton();
			upload.setText(I18NMessages.getString("UploadFrame.Load_files")); 

			upload.addActionListener( new ActionListener() {
					public void actionPerformed(ActionEvent e) {
					upload.setEnabled(false);
					upl = new FtpUploader(UploadFrame.this);
					Thread uploadThread = new Thread(upl);
					uploadThread.start();
				}
			});
		}
		return upload;
	}

	public synchronized void enableUpload() {
		upload.setEnabled(true);
	}

	/**
	 * This method initializes jButtonsPanel
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getJButtonsPanel() {
		if (jButtonsPanel == null) {
			BorderLayout borderLayout = new BorderLayout();
			borderLayout.setHgap(0);
			jStatusLabel = new JLabel();
			jStatusLabel.setText(I18NMessages.getString("UploadFrame.DragDrop_a_file_or_a_folder_to_begin")); 
			jButtonsPanel = new JPanel();
			jButtonsPanel.setLayout(borderLayout);
			jButtonsPanel.add(getUpload(), BorderLayout.EAST);
			jButtonsPanel.add(getJStatusPanel(), BorderLayout.CENTER);
		}
		return jButtonsPanel;
	}

	/**
	 * This method initializes jProgressBar
	 *
	 * @return javax.swing.JProgressBar
	 */
	public JProgressBar getJProgressBar() {
		if (jProgressBar == null) {
			jProgressBar = new JProgressBar();
		}
		return jProgressBar;
	}

	/**
	 * This method initializes jStatusPanel
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getJStatusPanel() {
		if (jStatusPanel == null) {
			jStatusPanel = new JPanel();
			jStatusPanel.setLayout(new BorderLayout());
			jStatusPanel.add(jStatusLabel, BorderLayout.NORTH);
			jStatusPanel.add(getJProgressBar(), BorderLayout.SOUTH);
		}
		return jStatusPanel;
	}

	/**
	 * This method initializes jItemsTree
	 *
	 * @return javax.swing.JTree
	 */
	public JTree getJItemsTree() {
		if (jItemsTree == null) {
			rootNode = new DefaultMutableTreeNode(I18NMessages.getString("UploadFrame.Items")); 
			treeModel = new DefaultTreeModel(rootNode);
			jItemsTree = new JTree(treeModel);
			jItemsTree.setRootVisible(false);
			jItemsTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
			jItemsTree.setShowsRootHandles(true);
			jItemsTree.setExpandsSelectedPaths(true);
			jItemsTree.addTreeSelectionListener(new TreeSelectionListener() {
				public void valueChanged(TreeSelectionEvent e) {
				}

			});

		}
		return jItemsTree;
	}

	public void setStatusText(String txt) {
		System.out.println(txt.toString());
	}
}
